#include "MyWifiMqtt.h"

//设置需要订阅的topic
String mySubTopics[] = {
  "my/esp/test1",
  "my/esp/test2"
};

// MQTT监听函数
void mqttCallback(char *topic, byte *payload, unsigned int length) {
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
  Serial.print("Message:");
  String charPayload;
  for (int i = 0; i < length; i++) {
    charPayload += (char) payload[i];
  }
  Serial.println(charPayload);
  Serial.println("-----------------------");
}

void setup() {
  //串口初始化
  Serial.begin(115200);
  //设置参数（该部分可以不设置）-在init之前
  setWifiHostname("esp-test");
  setMqttClientId("esp-test");
  setMqttUser("root", "test");
  //初始化-必须初始化
  initWifiMqtt("cyqand", "12345678", "192.168.1.200", 1883, "my/esp/online", mySubTopics, 2, mqttCallback);
  //获取连接状态（此部分可以不设置）
  if(wifiState()){
    Serial.println("wifi连接成功。");
  }
  if(mqttState()){
    Serial.println("mqtt连接成功。");
  }
}

void loop() {
  //mqtt循环获取值
  mqttLoop();
}
